package my.graduation.util;


import lombok.Data;
import my.graduation.vo.RegistrationRecordVO;

import java.util.concurrent.PriorityBlockingQueue;
import java.util.Comparator;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * 挂号队列
 * @Author: RavenLyn
 * @Date: 2024-12-23
 */
@Data
public class RegQueueManager {
    private List<Queue<RegistrationRecordVO>> normalQueues; // 每个时段一个正常队列
    private Queue<RegistrationRecordVO> suspendedQueue; // 总挂起队列
    private Integer currentShift;

    public RegQueueManager(int shift) {
        normalQueues = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            normalQueues.add(new LinkedList<>());
        }
        suspendedQueue =  new LinkedList<>(); // 初始化挂起队列
        currentShift = shift;
    }


    // 给该timeSlot的正常队列add record。
    public void addRecordToNormalQueue(int timeSlot, RegistrationRecordVO record) {
        if(timeSlot >= 6){
            timeSlot = timeSlot - 6;
        }
        normalQueues.get(timeSlot).add(record);
    }

    // 消费timeSlot对应的正常队列
    public RegistrationRecordVO callRecordFromNormalQueue(int timeSlot) {
        if(timeSlot >= 6){
            timeSlot = timeSlot - 6;
        }
        if (!normalQueues.get(timeSlot).isEmpty()) {
            return normalQueues.get(timeSlot).poll();
        }
        return null;
    }

    // 挂起这个号
//    public void suspendRecord(RegistrationRecordVO record) {
////        record.setStatus("SUSPENDED");
//        suspendedQueue.offer(record);
//    }

    public RegistrationRecordVO callRecordFromSuspendedQueue() {
        if (!suspendedQueue.isEmpty()) {
            return suspendedQueue.poll();
        }
        return null;
    }

    // 检查当前时段并更新Record状态
//    public void checkAndProcess(int timeSlot) {
//        RegistrationRecordVO record;
//        while ((record = callRecordFromNormalQueue(timeSlot)) != null) {
////            record.setStatus("CALLED");
//            // 处理叫号逻辑...
//        }
//        // 如果当前时段叫号完成且未到下一个时段，从挂起队列叫号
//        if (timeSlot < 5) {
//            while ((record = callRecordFromSuspendedQueue()) != null) {
////                record.setStatus("CALLED");
//                // 处理叫号逻辑...
//            }
//        }
//    }
}
